//
//  ModifyPasswordViewController.m
//  dangdang
//
//  Created by air on 16/1/25.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "QMSSGlobal.h"

@interface ModifyPasswordViewController ()

@end

@implementation ModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buttonLayout];
    
    _passwordTextField.delegate = self;
    _confirmPasswordTextField.delegate = self;
    _passwordTextField.secureTextEntry = YES;
    _confirmPasswordTextField.secureTextEntry = YES;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self.view addGestureRecognizer:singleTap];
    
    self.title = _naviTitle;
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonLayout{
    _confirmButton.layer.cornerRadius = 8;
}

- (IBAction)deleteOrConfirmButton:(UIButton *)sender {
    if (sender.tag == 0) {
        _passwordTextField.text = @"";
    }else{
        if ([_passwordTextField.text isEqual:nil] || [_passwordTextField.text isEqualToString:@""]) {
            [self alertViewShowWithAlertString:@"密码不能为空"];
            return;
        }
        if ([_confirmPasswordTextField.text isEqual:nil] || [_confirmPasswordTextField.text isEqualToString:@""]) {
            [self alertViewShowWithAlertString:@"确认密码不能为空"];
            return;
        }
        if (![_confirmPasswordTextField.text isEqualToString:_passwordTextField.text]) {
            [self alertViewShowWithAlertString:@"确认密码与密码不一致"];
            return;
        }
        
        NSString * passWordString = _passwordTextField.text;
        NSString * phoneString = userRegister.userPhone;
        NSString * token = userObject.accessToken;
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:token forKey:@"token"];
        [params setObject:@"0" forKey:@"uid"];
        [params setObject:@"" forKey:@"uname"];
        [params setObject:phoneString forKey:@"uphone"];
        [params setObject:passWordString forKey:@"upasswd"];
        [params setObject:@"" forKey:@"oldpasswd"];
        [params setObject:@"" forKey:@"alias"];
        [params setObject:@"0" forKey:@"udevice"];
        [params setObject:@"" forKey:@"usex"];
        
        [self postRequestWithMethodName:@"user/modify" andParams:params andSender:nil];
        [LoadingView showInView:self.view image:nil info:@"通讯中，请稍候..." animated:YES modeled:YES autoHide:NO];
    }
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([code isEqualToString:@"0"]) {
            userObject.userID = [NSString stringWithFormat:@"%@", [result objectForKey:@"retmsg"]];;
            NSString * plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"usersList.plist"];
            NSArray * array = [NSArray arrayWithObject:userObject.userID];
            [array writeToFile:plistPath atomically:YES];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [self alertViewShowWithAlertString:@"注册失败"];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
